package com.ww.springboot.boot.spring.smartlifecycle;

import org.springframework.context.SmartLifecycle;

import javax.annotation.PostConstruct;

/**
 *  SmartLifecycle  bean完成初始化后执行
 *
 * @author wanwei
 * @since 2021-02-19 11:52
 */
public class SmartLifecycleTestBean implements SmartLifecycle {


    private static Boolean isRunning = false;


    /**
     * 是否开启自动start方法
     * 1.是 则start()方法执行
     *
     * @return
     */
    @Override
    public boolean isAutoStartup() {
        return true;
    }

    @Override
    public void stop(Runnable callback) {
        System.out.println("--------------stop-----------runnable" + System.currentTimeMillis());
        isRunning = false;
    }

    @Override
    public void start() {
        System.out.println("--------------start-----------" + System.currentTimeMillis());

    }

    @Override
    public void stop() {
        System.out.println("--------------stop-----------" + System.currentTimeMillis());
    }


    /**
     * 是否活跃中
     * 1.是 则start()方法不执行
     * 2.否 则stop()方法不执行
     *
     * @return
     */
    @Override
    public boolean isRunning() {
        return isRunning;
    }


    public void setRunning(Boolean running) {
        isRunning = running;
    }

    @Override
    public int getPhase() {
        return 0;
    }

    /**
     * 初始化
     */
    @PostConstruct
    private void init() {
        System.out.println("--------------init-------------" + System.currentTimeMillis());
    }


}
